package cn.aishua.ui.myKaka;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.aishua.R;
import cn.aishua.data.CreditInfo;
import cn.aishua.network.HttpRequestUtil;
import cn.aishua.ui.myKaka.task.DataExchangeAsyncTask;

public class MyCreditActivity
    extends ListActivity
{

    private ArrayList<CreditInfo> creditList;

    private TextView tvAvailableCredits;
    private TextView tvUnavailableCredits;

    // page header widgets
    private ImageView ivLeftBtn;
    private ImageView ivRightBtn;
    private TextView tvTitile;

    private void initPageHeader()
    {
        // left button
        ivLeftBtn = (ImageView) findViewById( R.id.page_header_iv_left_btn );
        ivLeftBtn.setImageResource( R.drawable.btn_back );
        ivLeftBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                onBackPressed();
            }
        } );

        // title
        tvTitile = (TextView) findViewById( R.id.page_header_tv_title );
        tvTitile.setText( "我的积分" );

        // right button
        ivRightBtn = (ImageView) findViewById( R.id.page_header_iv_right_btn );
        ivRightBtn.setVisibility( View.GONE );

    }

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        Log.d( "state of activity", "onCreate( Bundle savedInstanceState )" );
        super.onCreate( savedInstanceState );
        setContentView( R.layout.my_kaka_my_credit );
        initPageHeader();
        initiate();
    }

    @Override
    protected void onResume()
    {
        Log.d( "state of activity", "onResume()" );
        super.onResume();

        new DataExchangeAsyncTask( this, "获取积分信息" )
        {

            @Override
            protected HttpResponse sendRequest( String... params )
            {
                return HttpRequestUtil.getInstance().getCreditAccounts();
            }

            @Override
            protected void handleSuccResponse( String strJson ) throws Exception
            {
                // TODO 
                //                strJson = MyKakaTestJsonData.CREDIT_ACCOUNTS;

                JSONObject jsonObject = new JSONObject( strJson );
                Log.d( "credit json res: ", jsonObject.toString() );
                JSONObject jsonUser = jsonObject.getJSONObject( "user" );
                JSONArray jsonLog = jsonObject.getJSONArray( "log" );
                String availableCredits = jsonUser.getString( "usable_credit" );
                String unavailableCredits = jsonUser.getString( "locked_credit" );
                tvAvailableCredits.setText( availableCredits );
                tvUnavailableCredits.setText( unavailableCredits );
                if( jsonLog != null && jsonLog.length() > 0 )
                {
                    creditList = new ArrayList<CreditInfo>();
                    int length = jsonLog.length();
                    Log.d( "credit", "size: " + length );
                    for( int i = 0; i < length; i++ )
                    {
                        JSONObject item = jsonLog.getJSONObject( i );
                        String vendor = item.getString( "store_name" );
                        Log.d( "credit", "vendor: " + vendor );
                        int amount = item.getInt( "money_amount" );
                        int credits = item.getInt( "consumer_credit" );
                        creditList.add( new CreditInfo( vendor, amount, credits ) );
                    }
                }
                ( (CreditAdapter) MyCreditActivity.this.getListView().getAdapter() ).notifyDataSetChanged();
            }

        }.execute();
    }

    private void initiate()
    {
        //        creditData = getIntent().getParcelableArrayListExtra( MemberCenterActivity.EXTRA_DATA_BUNDLE_CREDIT_LIST );
        tvAvailableCredits = (TextView) findViewById( R.id.my_kaka_my_credit_available_credits );
        tvUnavailableCredits = (TextView) findViewById( R.id.my_kaka_my_credit_unavailable_credits );

        ListView lvCreditList = getListView();
        lvCreditList.setDivider( null );
        CreditAdapter creditAdapter = new CreditAdapter();
        lvCreditList.setAdapter( creditAdapter );
    }

    public void toCreditExchangePage( View view )
    {
        Intent toCreditExchangePage = new Intent( this, CreditExchangeActivity.class );

        startActivity( toCreditExchangePage );
    }

    public void back( View view )
    {
        onBackPressed();
    }

    class ViewHolder
    {
        public TextView tvVendor;
        public TextView tvAmount;
        public TextView tvCredit;
    }

    class CreditAdapter
        extends BaseAdapter
    {

        public int getCount()
        {
            if( creditList != null )
            {
                return creditList.size();
            }
            else
            {
                return 0;
            }
        }

        public Object getItem( int position )
        {
            return creditList.get( position );
        }

        public long getItemId( int position )
        {
            return position;
        }

        public View getView( int position, View convertView, ViewGroup parent )
        {
            Log.d( "CreditAdapter", "getView()" );
            ViewHolder viewHolder = null;
            LayoutInflater inflater = LayoutInflater.from( MyCreditActivity.this );

            if( convertView == null )
            {
                convertView = inflater.inflate( R.layout.credit_item, null );

                // This holder contains three widgets of credit item
                viewHolder = new ViewHolder();
                viewHolder.tvVendor = (TextView) convertView.findViewById( R.id.my_kaka_my_credit_tv_vendor );
                viewHolder.tvAmount = (TextView) convertView.findViewById( R.id.my_kaka_my_credit_tv_amount );
                viewHolder.tvCredit = (TextView) convertView.findViewById( R.id.my_kaka_my_credit_tv_credit );

                convertView.setTag( viewHolder );
            }
            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.tvVendor.setText( creditList.get( position ).getVendor() );
            viewHolder.tvAmount.setText( "" + creditList.get( position ).getAmount() );
            viewHolder.tvCredit.setText( "" + creditList.get( position ).getCredit() );

            // set background
            if( position % 2 == 1 )
            {
                convertView.setBackgroundColor( getResources().getColor( R.color.white ) );
            }
            else
            {
                convertView.setBackgroundColor( getResources().getColor( R.color.lightgrey ) );

            }

            return convertView;
        }

    }
}
